package com.zkld.controller;

import com.github.pagehelper.PageInfo;
import com.zkld.pojo.Plugin;
import com.zkld.pojo.Result;
import com.zkld.pojo.User;
import com.zkld.service.PluginService;
import com.zkld.service.UserService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@Controller
@RequestMapping("/plugin")
public class PluginController {

    @Autowired
    private PluginService pluginService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public String query(@PathVariable("id") Integer id,Model model){
        Plugin plugin = new Plugin();
        plugin.setId(id);
        plugin = pluginService.query(plugin);
        model.addAttribute("plugin",plugin);
        return "plugin/details";
    }

    @GetMapping("/all")
    public String queryAll(@RequestParam(value = "p",defaultValue = "1",required = false) Integer p,
                           @RequestParam(value = "name",required = false) String name,
                           @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                           Model model){

        PageInfo<Plugin> plugins = pluginService.queryByPageAndName(p,name,pageSize);
        model.addAttribute("list",plugins);
        model.addAttribute("pageSize",pageSize);
        return "plugin/home";
    }

    @GetMapping("/add")
    public String addPlugin(Model model){
        List<User> list = userService.queryAll();
        model.addAttribute("list",list);
        return "plugin/add";
    }

    @PostMapping("/add")
    public String addPlugin(@RequestParam("file") MultipartFile file, @RequestParam("pluginFile") MultipartFile pluginFile,Plugin plugin){
        pluginService.add(plugin,file,pluginFile);
        return "redirect:/plugin/all";
    }

    @GetMapping("/del")
    @ResponseBody
    public Result delById(@RequestParam(value = "id",required = false) Integer id){
        pluginService.deleteById(id);
        Result result = new Result();
        result.setState("success");
        return result;
    }

    @GetMapping("/download")
    public void download(HttpServletResponse resp, @RequestParam(value="filePath",required = false) String filePath) throws Exception {
        //将filePath前面的“D:/***” 去掉 留下****.jpg
        String fileName = filePath.substring(10);
        InputStream in = new FileInputStream(new File(filePath));
        OutputStream out = resp.getOutputStream();
        resp.setContentType("application/octet-stream");
        resp.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        IOUtils.copy(in, out);
        out.flush();
        out.close();
        in.close();
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable("id")Integer id, Model model){
        Plugin plugin = new Plugin();
        plugin.setId(id);
        model.addAttribute("plugin",pluginService.query(plugin));
        model.addAttribute("list",userService.queryAll());
        return "plugin/update";
    }

    @PostMapping("/update")
    public String update(Plugin plugin){
        pluginService.update(plugin);
        return "redirect:/plugin/all";
    }

}
